#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<sys/time.h>
#include <string.h>
#include <dirent.h>

unsigned long get_file_size(const char *path)
{
    unsigned long filesize = -1;
    FILE *fp;
    fp = fopen(path, "r");
    if(fp == NULL)
        return filesize;
    fseek(fp, 0L, SEEK_END);
    filesize = ftell(fp);
    fclose(fp);
    return filesize;
}

int hasSuffixLMZ(char *name)
{
	while( *name!='\0' )
		name++;
	name-=4;
	if( *name=='.' && *(name+1)=='l' && *(name+2)=='m' && *(name+3)=='z' )
		return 1;

	return 0;
}

int readFileList(char *basePath)
{
    struct timeval tpstart;
    struct timeval tpend;
    long timedif,timedif2;
    char cmd_str[200],tmp1[200],tmp2[200];

    DIR *dir;
    struct dirent *ptr;
    char base[1000];

   	FILE *fp;
	fp= fopen("log.txt","wt+");
	if( fp == NULL )
	{
		printf("cannot open log.txt");
		return 0;
	}

    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }
    sprintf(tmp1,"%-22s%20s%10s%15s%15s%15s%15s\n","fileName","FileSize","ZipTime","UnzipTime","ZTime/Size","UZTime/Size","CompressRate");
    fputs(tmp1,fp);

    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        else if(ptr->d_type == 8 ||ptr->d_type == 10||ptr->d_type == 4)
	{  ///file ,link file, dir
		printf("d_name:%s/%s\n",basePath,ptr->d_name) ;
		if( hasSuffixLMZ(ptr->d_name) )
			continue;

		sprintf(cmd_str,"./zz -e %s/%s %s/%s.lmz", basePath,ptr->d_name,basePath,ptr->d_name );
		sprintf(tmp1,"%s/%s",basePath,ptr->d_name );
		sprintf(tmp2,"%s/%s.lmz",basePath,ptr->d_name);

		gettimeofday(&tpstart, NULL);
		system(cmd_str);
		gettimeofday(&tpend, NULL);
		timedif = 1000000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_usec-tpstart.tv_usec)/1000;

        unsigned long size_= get_file_size(tmp2);

		sprintf(cmd_str,"./zz -d %s/%s %s/%s.lmz ", basePath,ptr->d_name,basePath,ptr->d_name );
		gettimeofday(&tpstart, NULL);
		system(cmd_str);
		gettimeofday(&tpend, NULL);
		timedif2 = 1000000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_usec-tpstart.tv_usec)/1000;

		printf("it took %ld microseconds to compress\n", timedif );
		printf("The compress rate is %lf\n", (double)size_/ get_file_size(tmp1) );
		printf("it took %ld microseconds to uncompress\n", timedif2 );

		char t[400];
		unsigned long size= get_file_size(tmp1) ;
		double cRate=  (double)size_/ get_file_size(tmp1);
		double tdiv1= (double)timedif/size, tdiv2= (double)timedif2/size;
		sprintf(t,"%-22s%20ld%10ld%15ld%15lf%15lf%15lf\n", tmp1,size,timedif,timedif2,tdiv1,tdiv2,cRate);
		fputs( t,fp);

        }
    }
    closedir(dir);
    return 1;
}

int evaluate()
{
	//system("ifconfig");
	//system("zip -r wen.zip Midterm");
	//unzip filename.zip
	//system("ps aux|more zip");
	  DIR *dir;
   	 char basePath[1000];

   	 ///get the current absoulte path
   	 memset(basePath,'\0',sizeof(basePath));
   	 getcwd(basePath, 999);
   	 printf("the current dir is : %s\n",basePath);

	memset(basePath,'\0',sizeof(basePath));
    	strcpy(basePath,"./testSet");
    	readFileList(basePath);
/*
	double cRate;

	FILE *fp;
	fp= fopen("log.txt","wt+");
	if( fp == NULL )
	{
		printf("cannot open log.txt");
		return 0;
	}
*/
	    ///get the file list

/*
	struct timeval tpstart;
        struct timeval tpend;
        long timedif,timedif2;

        gettimeofday(&tpstart, NULL);
	system("zip -r wen.zip nachos");
	gettimeofday(&tpend, NULL);

        timedif = 1000000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_usec-tpstart.tv_usec)/1000;
	printf("it took %ld microseconds\n to compress", timedif );
	printf("The compress rate is %lf\n", (double)get_file_size("wen.zip")/ get_file_size("nachos") );

	gettimeofday(&tpstart, NULL);
	system("unzip -f wen.zip");
	gettimeofday(&tpend, NULL);
        timedif2 = 1000000*(tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_usec-tpstart.tv_usec)/1000;

	printf("it took %ld microseconds to compress\n", timedif );
	printf("The compress rate is %lf\n", (double)get_file_size("wen.zip")/ get_file_size("nachos") );
	printf("it took %ld microseconds to uncompress\n", timedif2 );
*/

	return 0;
}
